package com.cheetah.start.module.system.controller;

import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.annotation.RepeatSubmit;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.common.utils.MyFileUtil;
import com.cheetah.start.module.system.domain.SysAdmin;
import com.cheetah.start.module.system.domain.SysFile;
import com.cheetah.start.module.system.service.impl.CommonService;
import com.cheetah.start.module.system.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 通用
 *
 * @author xjhmwj
 * @date 2021/6/11 17:24
 */
@Api(tags = "通用上传")
@Slf4j
@RestController
@RequestMapping("/system/common")
public class CommonController extends ResultController {

    @Autowired
    private CommonService commonService;
    @Autowired
    private UploadService localUploadServiceImpl;
    @Autowired
    private UploadService aliOssUploadServiceImpl;

    /**
     * 本地上传
     *
     * @param file 文件
     * @return {@link Result}
     */
    @Log(title = "上传", describe = "本地上传", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    @ApiOperation(value = "本地上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        MyFileUtil.checkCommonSuffix(file);
        SysFile sysFile = localUploadServiceImpl.upload(file);
        commonService.asyncRecord(sysFile);
        return success(commonService.getResultData(sysFile));
    }


    /**
     * 上传阿里 oss
     *
     * @param file 文件
     * @return {@link Result}
     */
    @Log(title = "上传", describe = "阿里oss上传", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    @ApiOperation(value = "阿里云 oss 上传")
    @PostMapping("/upload/ossAliYun")
    public Result uploadAliOss(MultipartFile file) throws IOException {
        MyFileUtil.checkCommonSuffix(file);
        SysFile sysFile = aliOssUploadServiceImpl.upload(file);
        commonService.asyncRecord(sysFile);
        return success(commonService.getResultData(sysFile));
    }


}
